%This scripts performs demonstration of the current project functionality.
% Project performs edge detection of input gray scale imaged. Several
% different detail extraction methods can be applied.
%
% References: 
% 
% See also: CONFIG, EXTRACT_DETAILS_FILTERS, EXTRACT_DETAILS_WAVELETS
%
% Created on:    2014-01-04 (Dragomir El Mezeni)
% Last revision: 2014-01-23 (Mohamed Marouf)  

%------------- BEGIN CODE --------------

config
close all
clc

disp('DEMO: Multiscale edge detection');

%% Parameter settings
verbose = 0;
add_noise = true;
noise_sigma = 0.05;

enable_pre_smoothing = false;
pre_smoothing_size = 7;
pre_smoothing_sigma = 1.5;
pre_smoothing_scaling = true;

% Edge extraction type:
%  1 - Different custom filters with smoothing and interpolation are used
%      for details extraction
%
%  2 - Wavelet decomposition is used for detail extraction
edge_extraction_type = 1;

higher_level = 2;
lower_level = 1;
threshold = 0.001;

%% Reading input image

I = double(imread('../images/lena_color.jpg')) / 255;

%% Adding artificial noise
if (add_noise)
    I = imnoise(I, 'gaussian', 0, noise_sigma);
end

I = rgb2yuv(I);

Iy = I(:,:,1);

figure('Name', 'Input image'); imshow(Iy);
title('Input image');

%% Pre smoothing with scaling
%%% Enable noise suppression before starting edge detection process.
%%% Scaling should prevent loss of details due to smoothing function.
if (enable_pre_smoothing)
    
    if (pre_smoothing_scaling)
        Iy = imresize(Iy,2);
    end
    
    Iy = imfilter(Iy, fspecial('gaussian', pre_smoothing_size, pre_smoothing_sigma));
    
    if (pre_smoothing_scaling)
        Iy = imresize(Iy, 0.5);
    end
end

%% Egde extraction

if (edge_extraction_type == 1)
    disp('    Edge extraction: Algorithm based on custom filters running');
    [Dh, Dv, Dd1, Dd2] = extract_details_marouf(Iy);
else
    disp('    Edge extraction: DWT based algorithm running');
    [Dh, Dv, Dd1,Dd2]= extract_details_wavelets(Iy);
end

if (verbose > 2)
    num_levels = size(Dh, 2);
    for i = 1:num_levels
        figure; showIm(Dh{i});
        figure; showIm(Dv{i});
        if (edge_extraction_type == 1)
            figure; showIm(Dd1{i});
            figure; showIm(Dd2{i});
        else
            figure; showIm(Dd{i});
        end
    end
end

%% Gradient extraction

[comb_img,comb_h,comb_v,comb_d1,comb_d2] = combination(Dv,Dh,Dd1,Dd2,'mult', lower_level, higher_level);

figure('Name', 'Output image before suppression');
imagesc(comb_img); title('Output image before suppression');

% Non maximum suppression
if ~isempty(comb_d2)
   [sG dir] = suppression_4_inputs(comb_img, abs(comb_h), abs(comb_v),comb_d1,comb_d2);
else
   [sG dir] = suppression_3_inputs(comb_img, comb_h, comb_v);
end

if (verbose > 0)
    figure('Name', 'Output image after suppression');
    imagesc(sG); title('Output image after suppression');
end

% Thresholding
sG(sG > threshold) = 255;
sG(sG <= threshold) = 0;

figure('Name', 'Output image after thresholding'); imshow(sG);
title('Output image after thresholding');

figure('Name', 'Output image of Canny edge detection'); imshow(edge(Iy,'canny'));
title('Output image of Canny edge detection');


